home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Visual Cafe Pro v1.0 / SOURCE.BIN / Label3D.java < prev    next >
Encoding:
Java Source  |  1997-06-19  |  13.6 KB  |  507 lines

  1. package symantec.itools.awt;
  2.  
  3.  
  4. import java.awt.Canvas;
  5. import java.awt.Dimension;
  6. import java.awt.Color;
  7. import java.awt.FontMetrics;
  8. import java.awt.Font;
  9. import java.awt.Rectangle;
  10. import java.awt.Graphics;
  11.  
  12. //    01/29/97    TWB    Integrated changes from Windows 
  13. //     01/29/97    TWB    Integrated changes from Macintosh
  14.  
  15. /**
  16.  * Creates a text string, with a three-dimensional visual effect, that is 
  17.  * usually attached to an option, box, or button.
  18.  * <p>
  19.  * An application or applet can change the label text string, but a user 
  20.  * cannot edit it.
  21.  * <p>
  22.  * Note: For most components, labels are usually created by specifying text 
  23.  * for the Label property of that component. 
  24.  * <p>
  25.  * @version 1.0, Nov 26, 1996
  26.  * @author Symantec
  27.  */
  28. public class Label3D
  29.     extends    Canvas
  30.     implements AlignStyle,
  31.                BevelStyle
  32. {
  33.     /**
  34.      * Constant indicating a drawing margin of 0 pixels around the inside of 
  35.      * the border.
  36.      */
  37.     public static final int INDENT_ZERO = 0;
  38.  
  39.     /**
  40.      * Constant indicating a drawing margin of 1 pixel around the inside of 
  41.      * the border.
  42.      */
  43.     public static final int INDENT_ONE = 1;
  44.  
  45.     /**
  46.      * Constant indicating a drawing margin of 2 pixels around the inside of 
  47.      * the border.
  48.      */
  49.     public static final int INDENT_TWO = 2;
  50.  
  51.     /**
  52.      * the label text.
  53.      */
  54.     protected String sLabel3D;
  55.  
  56.  
  57.     private int alignStyle;
  58.     private int bevelStyle;
  59.     private Color color1;
  60.     private Color color2;
  61.     private Color textColor;
  62.     private Color borderedColor;
  63.     private FontMetrics fm;
  64.     private int xTemp;
  65.     private int yTemp;
  66.     private int indent = INDENT_ZERO;
  67.     private boolean bOsFlag = false;
  68.  
  69.  
  70.     /**
  71.      * Constructs a Label3D with black text, center aligned, raised border, 
  72.      * no text, and a zero border indent.
  73.      */
  74.     public Label3D()
  75.     {
  76.         this("", ALIGN_CENTERED, BEVEL_RAISED, Color.black, INDENT_ZERO);
  77.     }
  78.  
  79.     /**
  80.      * Constructs a Label3D with black text, azero border indent, and the 
  81.      * specified text alignment, and bevel style.
  82.      * @param sText text of the Label3D
  83.      * @param alignStyle text alignment style: ALIGN_LEFT, ALIGN_CENTERED, or ALIGN_RIGHT
  84.      * @param bevelStyle bevel style: BEVEL_RAISED, BEVEL_LOWERED, BEVEL_LINE, or BEVEL_NONE
  85.      */
  86.     public Label3D(String sText, int alignStyle, int bevelStyle)
  87.     {
  88.         this(sText, alignStyle, bevelStyle, Color.black, INDENT_ZERO);
  89.     }
  90.  
  91.     /**
  92.      * Constructs a Label3D with a zero border indent and the specified attributes.
  93.      * @param sText text of the Label3D
  94.      * @param alignStyle text alignment style: ALIGN_LEFT, ALIGN_CENTERED, or ALIGN_RIGHT
  95.      * @param bevelStyle bevel style: BEVEL_RAISED, BEVEL_LOWERED, BEVEL_LINE, or BEVEL_NONE
  96.      * @param color the label text color
  97.      */
  98.     public Label3D(String sText, int alignStyle, int bevelStyle, Color color)
  99.     {
  100.         this(sText, alignStyle, bevelStyle, color, INDENT_ZERO);
  101.     }
  102.  
  103.     /**
  104.      * Constructs a Label3D with black label text and the specified attributes.
  105.      * @param sText text of the Label3D
  106.      * @param alignStyle text alignment style: ALIGN_LEFT, ALIGN_CENTERED, or ALIGN_RIGHT
  107.      * @param bevelStyle bevel style: BEVEL_RAISED, BEVEL_LOWERED, BEVEL_LINE, or BEVEL_NONE
  108.      * @param indent the amount to indent around the border: INDENT_ZERO, 
  109.      * INDENT_ONE,  or INDENT_TWO
  110.      */
  111.     public Label3D(String sText, int alignStyle, int bevelStyle, int indent)
  112.     {
  113.         this(sText, alignStyle, bevelStyle, Color.black, indent);
  114.     }
  115.  
  116.     /**
  117.      * Constructs a Label3D with the specified attributes.
  118.      * @param sText text of the Label3D
  119.      * @param alignStyle text alignment style: ALIGN_LEFT, ALIGN_CENTERED, or ALIGN_RIGHT
  120.      * @param bevelStyle bevel style: BEVEL_RAISED, BEVEL_LOWERED, BEVEL_LINE, or BEVEL_NONE
  121.      * @param color the label text color
  122.      * @param indent the amount to indent around the border: INDENT_ZERO, 
  123.      * INDENT_ONE,  or INDENT_TWO
  124.      */
  125.     public Label3D(String sText, int alignStyle, int bevelStyle, Color color, int indent)
  126.     {
  127.         String sOs = System.getProperty("os.name");
  128.  
  129.         if (sOs.startsWith("S") ||      // SunOS, Solaris
  130.             sOs.startsWith("OSF") )     // OSF
  131.         {
  132.             bOsFlag = true;
  133.             setFont(new Font("Dialog", Font.PLAIN, 10));
  134.         }
  135.         else
  136.         {
  137.             bOsFlag = false;
  138.         }
  139.  
  140.         sLabel3D = sText;
  141.         textColor = color;
  142.         borderedColor = Color.black;
  143.  
  144.         setBorderIndent(indent, false);
  145.         setAlignStyle(alignStyle);
  146.         setBevelStyle(bevelStyle);
  147.     }
  148.  
  149.  
  150.     //--------------------------------------------------
  151.     // accessor methods
  152.     //--------------------------------------------------
  153.  
  154.     /**
  155.      * Sets the alignment style.
  156.      * @param style text alignment style: ALIGN_LEFT, ALIGN_CENTERED, or ALIGN_RIGHT
  157.      * @see #getAlignStyle
  158.      */
  159.     public void setAlignStyle(int style)
  160.     {
  161.         alignStyle = style;
  162.         invalidate();
  163.     }
  164.  
  165.     /**
  166.      * Gets the current alignment style.
  167.      * @return text alignment style: ALIGN_LEFT, ALIGN_CENTERED, or ALIGN_RIGHT
  168.      * @see #setAlignStyle
  169.      */
  170.     public int getAlignStyle()
  171.     {
  172.         return alignStyle;
  173.     }
  174.  
  175.     /**
  176.      * Sets the border style.
  177.      * @param style border bevel style: BEVEL_RAISED, BEVEL_LOWERED, BEVEL_LINE, or BEVEL_NONE
  178.      * @see #getBevelStyle
  179.      */
  180.     public void setBevelStyle(int style)
  181.     {
  182.         bevelStyle = style;
  183.  
  184.         switch (style)
  185.         {
  186.             case BEVEL_LOWERED :
  187.             {
  188.                 color1 = Color.black;
  189.                 color2 = Color.white;
  190.                 break;
  191.             }
  192.  
  193.             case BEVEL_RAISED :
  194.             {
  195.                 color1 = Color.white;
  196.                 color2 = Color.black;
  197.                 break;
  198.             }
  199.  
  200.             case BEVEL_LINE :
  201.             {
  202.                 color1 = borderedColor;
  203.                 color2 = borderedColor;
  204.                 break;
  205.             }
  206.  
  207.             default: // catches any bogus type, paint(...) relies on color1 being null
  208.             {
  209.                 color1 = color2 = null;
  210.                 break;
  211.             }
  212.         }
  213.  
  214.         invalidate();
  215.     }
  216.  
  217.     /**
  218.      * Gets the current border style.
  219.      * @return border bevel style: BEVEL_RAISED, BEVEL_LOWERED, BEVEL_LINE, or BEVEL_NONE
  220.      * @see #setBevelStyle
  221.      */
  222.     public int getBevelStyle()
  223.     {
  224.         return bevelStyle;
  225.     }
  226.  
  227.     /**
  228.      * Sets the border indent amount.
  229.      * @param indent the amount to indent around the border: INDENT_ZERO, 
  230.      * INDENT_ONE,  or INDENT_TWO
  231.      * @see #getBorderIndent
  232.      */
  233.     public void setBorderIndent(int indent)
  234.     {
  235.         setBorderIndent(indent, true);
  236.     }
  237.  
  238.     /**
  239.      * Gets the current border indent amount.
  240.      * @return the amount currently indented around the border: INDENT_ZERO, 
  241.      * INDENT_ONE,  or INDENT_TWO
  242.      * @see #setBorderIndent
  243.      */
  244.     public int getBorderIndent()
  245.     {
  246.         return indent;
  247.     }
  248.  
  249.     /**
  250.      * Sets the border color.
  251.      * @param color color to use for the border
  252.      */
  253.     public void setBorderedColor(Color color)
  254.     {
  255.         borderedColor = color;
  256.  
  257.         if (bevelStyle == BEVEL_LINE)
  258.         {
  259.             color1 = color;
  260.             color2 = color;
  261.         }
  262.  
  263.         invalidate();
  264.     }
  265.  
  266.     /**
  267.      * Sets the label text.
  268.      * @param sLabel the new label text
  269.      * @see #getText
  270.      */
  271.     public void setText(String sText)
  272.     {
  273.         sLabel3D = sText;
  274.         repaint();
  275.     }
  276.  
  277.     /**
  278.      * Gets the current label text.
  279.      * @return the current label text
  280.      * @see #setText
  281.      */
  282.     public String getText()
  283.     {
  284.         return sLabel3D;
  285.     }
  286.  
  287.     /**
  288.      * Sets the label text color.
  289.      * @param color the new color for the label text
  290.      * @see #getTextColor
  291.      */
  292.     public void setTextColor(Color color)
  293.     {
  294.         textColor = color;
  295.         invalidate();
  296.     }
  297.  
  298.     /**
  299.      * Gets the current label text color.
  300.      * @return the current color of the label text
  301.      * @see #setTextColor
  302.      */
  303.     public Color getTextColor()
  304.     {
  305.         return textColor;
  306.     }
  307.  
  308.     /**
  309.      * Paints this component using the given graphics context.
  310.      * This is a standard Java AWT method which typically gets called
  311.      * by the AWT to handle painting this component. It paints this component
  312.      * using the given graphics context. The graphics context clipping region
  313.      * is set to the bounding rectangle of this component and its <0,0>
  314.      * coordinate is this component's top-left corner.
  315.      * 
  316.      * @param g the graphics context used for painting
  317.      * @see java.awt.Component#repaint
  318.      * @see java.awt.Component#update
  319.      */
  320.     public void paint(Graphics g)
  321.     {
  322.         Rectangle r;
  323.         Color c;
  324.  
  325.         r = bounds();
  326.         c = g.getColor();
  327.  
  328.         g.setColor(getBackground());
  329.         g.fillRect(0, 0, r.width - 1, r.height - 1);
  330.  
  331.         if (color1 != null)
  332.         {
  333.             g.clipRect(0, 0, r.width, r.height);
  334.  
  335.             // top
  336.             g.setColor(color1);
  337.             g.drawLine(1+indent, indent, r.width-3-indent, indent);
  338.  
  339.             // bottom
  340.             g.setColor(color2);
  341.             g.drawLine(1+indent, r.height-1-indent, r.width-3-indent, r.height-1-indent);
  342.  
  343.             // left
  344.             g.setColor(color1);
  345.             g.drawLine(indent, indent, indent, r.height-1-indent);
  346.  
  347.             // right
  348.             g.setColor(color2);
  349.             g.drawLine(r.width-2-indent, indent, r.width-2-indent, r.height-1-indent);
  350.  
  351.             g.clipRect(2 + indent, 1+indent, r.width-9-indent, r.height-4-indent);
  352.             yTemp = 1 + indent;
  353.         }
  354.         else
  355.         {
  356.             g.drawRect(indent, indent, r.width-2-indent, r.height-1-indent);
  357.             g.clipRect(2, 1, r.width-7, r.height-2);
  358.             yTemp = 1;
  359.         }
  360.  
  361.         // text
  362.         g.setColor(textColor);
  363.  
  364.         fm = getFontMetrics(getFont());
  365.         yTemp = (r.height - yTemp + fm.getAscent()) / 2;
  366.  
  367.         switch (alignStyle)
  368.         {
  369.             case ALIGN_LEFT:
  370.             {
  371.                 if (bevelStyle == BEVEL_LINE)
  372.                 {
  373.                     g.drawString(sLabel3D, 4, yTemp);
  374.                 }
  375.                 else
  376.                 {
  377.                     g.drawString(sLabel3D, 8, yTemp);
  378.                 }
  379.  
  380.                 break;
  381.             }
  382.  
  383.             case ALIGN_RIGHT:
  384.             {
  385.                 xTemp = r.width - fm.stringWidth(sLabel3D);
  386.  
  387.                 if (bevelStyle == BEVEL_LINE)
  388.                 {
  389.                     g.drawString(sLabel3D, xTemp - 6, yTemp);
  390.                 }
  391.                 else
  392.                 {
  393.                     g.drawString(sLabel3D, xTemp - 10, yTemp);
  394.                 }
  395.  
  396.                 break;
  397.             }
  398.  
  399.             case ALIGN_CENTERED:
  400.             {
  401.                 xTemp = (r.width - fm.stringWidth(sLabel3D)) / 2;
  402.                 g.drawString(sLabel3D, xTemp, yTemp);
  403.  
  404.                 break;
  405.             }
  406.         }
  407.  
  408.         // reset color
  409.         g.setColor(c);
  410.     }
  411.  
  412.     /**
  413.      * Returns the recommended dimensions to properly display this component.
  414.      * This is a standard Java AWT method which gets called to determine
  415.      * the recommended size of this component. 
  416.      *
  417.      * @see #minimumSize
  418.      */
  419.     public Dimension preferredSize()
  420.     {
  421.         Dimension s = size();
  422.         Dimension m = minimumSize();
  423.  
  424.         return new Dimension(Math.max(s.width, m.width), Math.max(s.height, m.height));
  425.     }
  426.  
  427.     /**
  428.      * Returns the minimum dimensions to properly display this component.
  429.      * This is a standard Java AWT method which gets called to determine
  430.      * the minimum size of this component. 
  431.      * 
  432.      * @see #preferredSize
  433.      */
  434.     public Dimension minimumSize()
  435.     {
  436.         Dimension min;
  437.         Font f;
  438.  
  439.         min = new Dimension(18, 10);
  440.         f   = getFont();
  441.  
  442.         if (f == null)
  443.         {
  444.             if (bOsFlag)
  445.             {
  446.                 min.height = 29;
  447.             }
  448.         }
  449.         else
  450.         {
  451.             fm = getFontMetrics(f);
  452.             min.width = fm.stringWidth(sLabel3D) + 18;
  453.             min.height = fm.getHeight() + 10;
  454.  
  455.             if (bOsFlag && min.height < 29)
  456.             {
  457.                 min.height = 29;
  458.             }
  459.         }
  460.  
  461.         return min;
  462.     }
  463.  
  464.     /**
  465.      * Moves and/or resizes this component.
  466.      * This is a standard Java AWT method which gets called to move and/or 
  467.      * resize this component. Components that are in containers with layout
  468.      * managers should not call this method, but rely on the layout manager
  469.      * instead.
  470.      * 
  471.      * @param x horizontal position in the parent's coordinate space
  472.      * @param y vertical position in the parent's coordinate space
  473.      * @param width the new width
  474.      * @param height the new height
  475.      */
  476.     public synchronized void reshape(int x, int y, int width, int height)
  477.     {
  478.         super.reshape(x, y, width, height);
  479.  
  480.         if (!isValid())
  481.         {
  482.             repaint();
  483.         }
  484.     }
  485.  
  486.     private void setBorderIndent(int indent, boolean bRepaint)
  487.     {
  488.         if (indent < INDENT_ZERO)
  489.         {
  490.             this.indent = INDENT_ZERO;
  491.         }
  492.         else if (indent > INDENT_TWO)
  493.         {
  494.             this.indent = INDENT_TWO;
  495.         }
  496.         else
  497.         {
  498.             this.indent = indent;
  499.         }
  500.  
  501.         if (bRepaint)
  502.         {
  503.             repaint();
  504.         }
  505.     }
  506. }
  507.